package org.cocos2dx.javascript.admob;

import android.app.Activity;
import android.util.Log;

import com.google.android.gms.ads.AdError;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.FullScreenContentCallback;
import com.google.android.gms.ads.LoadAdError;
import com.google.android.gms.ads.interstitial.InterstitialAd;
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback;
import com.google.android.gms.ads.rewarded.RewardedAd;
import com.logicgame.carmerge.R;

import androidx.annotation.NonNull;

public class Interstitial {
    private static InterstitialAd mInterstitialAd;
    static Activity app;
    private static final String TAG = "Admob";
    //加载InterstitialAd
    public static void InitInterstitialAD(final Activity _app){
        app = _app;
        final String INTERSTITIAL_AD_UNIT_ID = app.getString(R.string.INTERSTITIAL_AD_UNIT_ID);
        AdRequest adRequest = new AdRequest.Builder().build();
        InterstitialAd.load(app,INTERSTITIAL_AD_UNIT_ID, adRequest,
                new InterstitialAdLoadCallback() {

                    @Override
                    public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
                        // The mInterstitialAd reference will be null until
                        // an ad is loaded.
                        mInterstitialAd = interstitialAd;
                        Log.i(TAG, "interstitialAd onAdLoaded");
                        interstitialAd.setFullScreenContentCallback(
                                new FullScreenContentCallback() {
                                    @Override
                                    public void onAdDismissedFullScreenContent() {
                                        // Called when fullscreen content is dismissed.
                                        // Make sure to set your reference to null so you don't
                                        // show it a second time.
                                        mInterstitialAd = null;
                                        Log.d("TAG", "The ad was dismissed.");
                                    }

                                    @Override
                                    public void onAdFailedToShowFullScreenContent(AdError adError) {
                                        // Called when fullscreen content failed to show.
                                        // Make sure to set your reference to null so you don't
                                        // show it a second time.
                                        mInterstitialAd = null;
                                        Log.d("TAG", "The ad failed to show.");
                                    }

                                    @Override
                                    public void onAdShowedFullScreenContent() {
                                        // Called when fullscreen content is shown.
                                        Log.d("TAG", "The ad was shown.");
                                    }
                                });
                        //ShowInterstitialAD();
                    }
                    @Override
                    public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
                        // Handle the error
                        Log.i(TAG, loadAdError.getMessage());
                        mInterstitialAd = null;
                        Ad_Fail_Timer.reload(2,app);
                    }

                });
    }
    public static void ShowInterstitialAD(){
        app.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Log.v(TAG,"show interAD in admob.interstitial");
                if (mInterstitialAd != null) {
                    mInterstitialAd.show(app);
                } else {
                    //Toast.makeText(this, "Ad did not load", Toast.LENGTH_SHORT).show();
                    //startGame();
                    InitInterstitialAD(app);
                }
            }
        });

    }
}
